package ynu.edu.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 熔断器降级处理控制器
 * 当服务不可用时提供降级响应
 */
@RestController
@RequestMapping("/fallback")
public class FallbackController {

    /**
     * Consumer服务降级处理
     */
    @GetMapping("/consumer")
    public ResponseEntity<Map<String, Object>> consumerFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 503);
        response.put("message", "🔥 Consumer服务熔断中！请稍后重试");
        response.put("service", "consumer-service");
        response.put("fallback", true);
        response.put("timestamp", System.currentTimeMillis());
        
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
    }

    /**
     * Provider服务降级处理
     */
    @GetMapping("/provider")
    public ResponseEntity<Map<String, Object>> providerFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 503);
        response.put("message", "🔥 Provider服务熔断中！返回默认数据");
        response.put("service", "provider-service");
        response.put("fallback", true);
        response.put("timestamp", System.currentTimeMillis());
        
        // 提供降级数据
        Map<String, Object> fallbackData = new HashMap<>();
        fallbackData.put("userId", 0);
        fallbackData.put("userName", "默认用户(熔断降级)");
        fallbackData.put("message", "服务熔断中，返回默认数据");
        
        response.put("data", fallbackData);
        
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
    }
} 